var usersStore = null;
var manageUsersGrid = null;

function searchUsers(isExport){

	var lastOptions = usersStore.lastOptions;
	
	var userName = userAutocompleteCombo.getValue();
	if(isExport){
		window.location.href = "./userList_export.jsp?userName="+encodeURI(userName);
		return;
	}
	Ext.apply(lastOptions.params, {
		userName:encodeURI(userName),
		action:'search'
	});
	//console.log(lastOptions);
	
	usersStore.reload(lastOptions);
	Ext.apply(usersStore.baseParams, lastOptions.params);
	
}

Ext.onReady(function(){

var proxy = new Ext.data.HttpProxy({
        method: 'GET',
        prettyUrls: false,
        url:'users.jsp' , // see options parameter for Ext.Ajax.request
        api: {
            // all actions except the following will use above url
            create  : 'users.jsp',
            update  : 'users.jsp'
        }
});
	// create the Data Store
usersStore = new Ext.data.JsonStore({
    root: 'models',
    totalProperty: 'totalCount',
    idProperty: 'modelId',
    remoteSort: true,
    autoLoad:false,
    baseParams:{userid:-1},
    fields: [
        'userid','name','realName', 'email', 'supervisorName','description','admin'
    ],

    // load using script tags for cross domain, if the data in on the same domain as
    // this page, an HttpProxy would be better
    proxy:proxy
});

usersStore.addListener("create", function(){
	//grid.getSelectionModel().selectFirstRow();
});
    // manually load local data
var options = {params:{start:0, limit:20}};
	
usersStore.load(options);
	
	var sm = new Ext.grid.CheckboxSelectionModel({
	    listeners: {
	    // On selection change, set enabled state of the removeButton
	    // which was placed into the GridPanel using the ref config
	    selectionchange: function(m) {
	        
	    }
	},
	singleSelect:false
	});
	
	function renderUser(val,o,  record){
		//console.log(arguments);
		return '<a href="#no" onClick="top.showUserDetails(this, \''+record.data.userid+'\')">'+val+'</a>';
	}
	
	// create the Grid
    var grid = new Ext.grid.GridPanel({
        store: usersStore,
        columns: [
            sm,
            {id:'userID',header: userIDTitle,renderer:renderUser,dataIndex:'name',sortable: false,width:100},
			//{id:'userName',header: userNameTitle,dataIndex:'realName',sortable: false,width:100},
			{id:'email',header: emailTitle,dataIndex:'email',sortable: false,width:180},
			{id:'supervisor',header: supervisorTitle,dataIndex:'supervisorName',sortable: false,width:90},
			{id:'adminTitle',header: adminTitle,dataIndex:'admin',sortable: false,width:90, renderer:function(val){return val?yes:no ; }},
			{id:'description',header: descTitle,dataIndex:'description',sortable: false,width:200}
        ],
        sm: sm,
        title: '',
        columnLines: false,
        stripeRows: false,
        height:470,
        width:800,
        viewConfig: {
            forceFit:true,
            enableRowBody:false,
            showPreview:false,
            autoScroll:false, 
            scrollOffset: 2,
            getRowClass : function(record, rowIndex, p, store){
                if(this.showPreview){
                    p.body = '<p>'+record.data.excerpt+'</p>';
                    return 'x-grid3-row-expanded';
                }
                return 'x-grid3-row-collapsed';
            }
        },
        // config options for stateful behavior
        enableDragDrop:false,
        cls:'grid-outline',
        enableHdMenu:false,
        autoFitColumns: true,
        // paging bar on the bottom
        bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: usersStore,
            displayInfo: true,
            displayMsg: displayMsg,
            emptyMsg: emptyMsg,
            beforePageText:beforePageText,
            items:[
                 '-',{
                pressed: false,
                enableToggle:true,
                text: showPreview,
                cls: 'x-btn-text-icon details',
                toggleHandler: function(btn, pressed){
                    var view = grid.getView();
                    view.showPreview = pressed;
                    view.refresh();
                }
            }]
        })

    });
    
    grid.render('grid-container');
    manageUsersGrid = grid;
}); 

